﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class ChangePartsSendDate : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.经销商_品情_配件发货_索赔发货, Permission.经销商_品情_配件发货_供应商发货, Permission.查看};
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltySvrActList"; }
        }

        protected int QltyId
        {
            get
            {
                return GetRequestValue<int>("QltyId", 0);
            }
        }
        private AgentQltyInfo _objQlty = null;
        protected AgentQltyInfo objQlty
        {
            get
            {
                if (_objQlty == null)
                {
                    _objQlty = AgentQltyController.GetByQltyId(QltyId);
                }
                return _objQlty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
            //权限判断
            //配件人员只能修改 配件需求为 索赔发货
            //IHISCE 服务内群 修改 为 供应商发货
            if (objQlty.ClaimState != ClaimState.品情已确认.ToString())
            {
                Alert.Show("此品情未确认,不能修改配件发货信息", "提示", MessageBoxIcon.Information, ActiveWindow.GetHidePostBackReference());
               
            }
            if (objQlty.PartsRequestType == "索赔发货")
            {
                CheckPowerWithButton(Permission.经销商_品情_配件发货_索赔发货, this.btnSaveClose);
            }
            if (objQlty.PartsRequestType == "供应商发货")
            {
                CheckPowerWithButton(Permission.经销商_品情_配件发货_供应商发货, this.btnSaveClose);
            }
            if (objQlty.PartsRequestType == "索赔结算" || objQlty.PartsRequestType == "无配件需求" || objQlty.PartsRequestType=="备用件发货")
            {
                CheckPowerFailWithButton(btnSaveClose);
            }
           // CheckPowerWithButton(Permission.经销商_品情_修改配件发货情况, this.btnSaveClose);

            this.ClaimsNumber.Text = objQlty.ClaimsNumber;
            this.IssueDate.Text = objQlty.IssueDate.ToString("yyyy-MM-dd");
            this.AgentName.Text = objQlty.AgentName;
            this.SerialNumber.Text = objQlty.SerialNumber;
            this.MachineType.Text = objQlty.MachineType;
           
            this.FaultDate.Text = objQlty.FaultDate.ToString("yyyy-MM-dd");
            this.WorkHour.Text = objQlty.WorkHour.ToString("F0");
            this.PartsRequestType.Text = objQlty.PartsRequestType;
            this.NoticeSendPartsDate.Text = objQlty.NoticeSendPartsDate.ToString("yyyy-MM-dd HH:mm");
            this.SupperSendDate.Text =objQlty.SupperSendDate==DateTime.MinValue?"": objQlty.SupperSendDate.ToString("yyyy-MM-dd");
            this.ERPSendDate.Text = objQlty.ERPSendDate == DateTime.MinValue ? "" : objQlty.ERPSendDate.ToString("yyyy-MM-dd");
            this.ERPSendDateTimePicker.Text = objQlty.ERPSendDate == DateTime.MinValue ? "" : objQlty.ERPSendDate.ToString("HH:mm");

            this.RealSendDate.Text = objQlty.RealSendDate == DateTime.MinValue ? "" : objQlty.RealSendDate.ToString("yyyy-MM-dd");
            this.RealSendDateTimePicker.Text = objQlty.RealSendDate == DateTime.MinValue ? "" : objQlty.RealSendDate.ToString("HH:mm");

            this.AgentRecoveryDate.Text = objQlty.AgentRecoveryDate == DateTime.MinValue ? "" : objQlty.AgentRecoveryDate.ToString("yyyy-MM-dd");
            this.AgentRecoveryDateTimePicker.Text = objQlty.AgentRecoveryDate == DateTime.MinValue ? "" : objQlty.AgentRecoveryDate.ToString("HH:mm");
            //this.ImgHY.ImageUrl = objQlty.ImgHY;
            //加载配件
            DataTable listParts = AgentQltyController.GetAllPartsAndLogisticsByQltyId(QltyId);         
            this.grdAgentQltyParts.DataSource = listParts;
            this.grdAgentQltyParts.DataBind();

            //处理配件需求,不同需求显示不同的输入情况
            if (objQlty.PartsRequestType == "索赔发货")
            {
                this.SupperSendDate.Text = "";
                this.SupperSendDate.Hidden = true;
                this.ERPSendDate.Hidden =false;
                string erpdate = AgentQltyController.GetERPSendDate(QltyId);
                erpdate = string.IsNullOrEmpty(erpdate) ? "" : Convert.ToDateTime(erpdate).ToString("yyyy-MM-dd");
                this.ERPSendDate.Text = erpdate;//从信息平台中的 索赔来往明细帐中获取
                this.RealSendDate.Hidden = false;
            }

            if (objQlty.PartsRequestType == "供应商发货")
            {
                this.SupperSendDate.Hidden = false;
                this.ERPSendDate.Text = "";
                this.RealSendDate.Text = "";
                this.ERPSendDate.Hidden = true;
                this.RealSendDate.Hidden = true;
            }

            if (objQlty.PartsRequestType == "索赔结算" || objQlty.PartsRequestType == "无配件需求" || objQlty.PartsRequestType == "备用件发货")
            {
                this.SupperSendDate.Text="";
                this.ERPSendDate.Text="";
                this.RealSendDate.Text = "";
                this.SupperSendDate.Hidden = true;
                this.ERPSendDate.Hidden = true;
                this.RealSendDate.Hidden = true;
            }

            this.ImgHY.Text = objQlty.ImgHY;
            List<AgentQltyImgInfo> listImg = AgentQltyController.GetAllImgByQltyId_1(QltyId);
            try
            {
                ///加载图片
              
                for (int imgIndex = 1; imgIndex <= 6; imgIndex++)
                {
                    //FineUI.FileUpload FileUpladControl = Panel4.FindControl("filePhoto" + imgIndex) as FineUI.FileUpload;                
                    FineUI.Image imgRelativelyControl = Panel4.FindControl("imgRelatively" + imgIndex) as FineUI.Image;
                    FineUI.TextBox ImgTitleControl = Panel4.FindControl("ImgTitle" + imgIndex) as FineUI.TextBox;

                    ImgTitleControl.Text = listImg[imgIndex - 1].Title;
                    imgRelativelyControl.ImageUrl = listImg[imgIndex - 1].IMGRelatively;
                }
            }
            catch { }
            try
            {
                // this.fileOther.Label = listImg[6].IMGRelatively;
                this.fileOtherDownLoad.NavigateUrl = listImg[6].IMGRelatively;
                // this.fileOtherDownLoad.Hidden = string.IsNullOrEmpty(listImg[6].IMGRelatively);
            }
            catch { }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            DateTime _SupperSendDate=this.SupperSendDate.Text.IsEmptyOrNull()?DateTime.MinValue:Convert.ToDateTime(this.SupperSendDate.Text);
            DateTime _ERPSendDate = this.ERPSendDate.Text.IsEmptyOrNull() ? DateTime.MinValue : Convert.ToDateTime(this.ERPSendDate.Text + " " + this.ERPSendDateTimePicker.Text);
            DateTime _RealSendDate = this.RealSendDate.Text.IsEmptyOrNull() ? DateTime.MinValue : Convert.ToDateTime(this.RealSendDate.Text + " " + this.RealSendDateTimePicker.Text);

            bool reVal = AgentQltyController.UpdatePartsSendDate(QltyId, _SupperSendDate, _ERPSendDate, _RealSendDate);
            if (reVal)
            {
                Alert.Show("已成功提交", "提示", MessageBoxIcon.Information, ActiveWindow.GetHidePostBackReference());
               // PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else {
                Alert.Show("提交失败,请联系IT部", "提示", MessageBoxIcon.Error);
            }
               // , DateTime ERPSendDate, DateTime RealSendDate
        }
        protected void grdAgentQltyParts_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView dr = e.DataItem as DataRowView;
            try
            {


                System.Web.UI.WebControls.Label lbSvrResult = grdAgentQltyParts.Rows[e.RowIndex].FindControl("lbU_InvoiceNum") as System.Web.UI.WebControls.Label;

                lbSvrResult.Text = SKuaiDi100Controller.GetKuaiDi100(dr["U_ExpressMode"].ToString(), dr["U_InvoiceNum"].ToString());

                //execlClaims.NavigateUrl = "javascript:openExecl(" + dr["QltyId"] + ")";
            }
            catch
            {

            }
        }
    }
}